<?php
if (!defined('MEDIAWIKI')) {
	die();
}
/**
 * Class file for the TinCanAPI extension
 *
 * @addtogroup Extensions
 * 
 * LICENSE AND CREDITS
 * Copyright Andrew Downes 2012
 * Thanks to Francis Avila for providing some of the more advanced code
 * (http://stackoverflow.com/questions/10696627/post-json-with-php-curl-to-a-tin-can-api-lrs)
 * Thanks to Rustici Software for providing Java and JavaScript examples
 * (https://twitter.com/timpmartin/status/204579884692017153)
 * 
 * This extension file is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License and the GNU General Public License as published by
the Free Software Foundation, version 3
 * 
 */

class TinCanAPIcouk {

	public static function pageView(OutputPage &$out, Skin &$skin) {
		global $wgUser;
		global $wgRequest;
		if ( $wgUser->getOption('tinCanAPIClientOn')) { 
		
			$auth = $wgUser->getOption('tinCanAPIClientLRSKey');
			$LRSURL = $wgUser->getOption('tinCanAPIClientLRSURL');
	
			//statement vars
			$actorname = $wgUser -> getName();
			$actorEmail = $wgUser -> getEmail(); 
			$pageURL = $wgRequest -> getFullRequestURL ();
			$pageName = $out -> getHTMLTitle(); 
			$pageDesc = 'A page on TinCanAPI.co.uk - home of the Tin Can API Community';
	
			$statement = TinCanAPIcouk::buildStatement($actorname, $actorEmail, $pageURL, $pageName, $pageDesc);
	
			list($resp, $meta) = TinCanAPIcouk::make_request($statement, $LRSURL, $auth);
	
			//var_export($resp);
			// Returned headers, including errors, are in $meta
		}

		return true;
	}


	public static function make_request($data, $url, $auth) {
		$streamopt = array(
	        'ssl' => array(
	            'verify-peer' => false,
	        ),
	        'http' => array(
	            'method' => 'POST',
	            'ignore_errors' => true,
	            'header' =>  array(
	                'Authorization: '.$auth,
	                'Content-Type: application/json',
	                'Accept: application/json, */*; q=0.01',
	            ),
	            'content' => json_encode($data),
	        ),
	    );		
		$context = stream_context_create($streamopt);
		$stream = fopen($url, 'rb', false, $context);
		$ret = stream_get_contents($stream);
		$meta = stream_get_meta_data($stream);
		if ($ret) {
			$ret = json_decode($ret);
		}
		return array($ret, $meta);
	}

	public static function buildStatement($actorname, $actorEmail, $pageURL, $pageName, $pageDesc) {
		$statement = array(
	        array(
	            'actor' => array(
	                'name' => array($actorname),
	                'mbox'  => array('mailto:' . $actorEmail),
	                'objectType' => 'Person',
	            ),
	            'verb' => 'experienced',
	            'object' => array(
	                'objectType' => 'link',
	                'id'=> $pageURL,
	                'definition' => array(
	                    'name' => array('en-US'=>$pageName, 'en-GB'=>$pageName ),
	                    'description' => array('en-US'=> $pageDesc,'en-GB'=> $pageDesc),
	                ),
	            ),
	        ),
	    );
		
		return $statement;

	}
	
	public static function addPrefences( $user, &$preferences ) {
		
		$preferences['tinCanAPIClientLRSinfo'] = array(
                'type' => 'info',
                'label-message' => 'tinCanClientLRSinfo-pref', 
                'section' => 'TinCanAPI/Client',
                'default' => 'Enabling this feature requires you to have entered an e-mail address and will publish this address to the selected LRS',
        );
		
        $preferences['tinCanAPIClientOn'] = array(
                'type' => 'toggle',
                'label-message' => 'tinCanClientOn-pref', 
                'section' => 'TinCanAPI/Client',
        );
		
		$preferences['tinCanAPIClientLRSURLinfo'] = array(
                'type' => 'info',
                'label-message' => 'tinCanClientLRSURLinfo-pref', 
                'section' => 'TinCanAPI/Client',
                'default' => 'https://cloud.scorm.com/ScormEngineInterface/TCAPI/public/statements',
        );
		
		$preferences['tinCanAPIClientLRSURL'] = array(
                'type' => 'text',
                'label-message' => 'tinCanClientLRSURL-pref', 
                'section' => 'TinCanAPI/Client',
                'default' => 'https://cloud.scorm.com/ScormEngineInterface/TCAPI/public/statements',
        );
		
		$preferences['tinCanAPIClientLRSKeyinfo'] = array(
                'type' => 'info',
                'label-message' => 'tinCanClientLRSKeyinfo-pref',
                'section' => 'TinCanAPI/Client',
                'default' => 'Basic VGVzdFVzZXI6cGFzc3dvcmQ=',
        );
		
		$preferences['tinCanAPIClientLRSKey'] = array(
                'type' => 'text',
                'label-message' => 'tinCanClientLRSKey-pref',
                'section' => 'TinCanAPI/Client',
                'default' => 'Basic VGVzdFVzZXI6cGFzc3dvcmQ=',
        );
		

 
        // Required return value of a hook function.
        return true;
	}

}
?>
